/**
 *
 * Copyright 2015 RSVP Technologies Inc. All rights reserved.
 * GeoPlaceCode.java
 *
 */
package com.gfinal.map.geo;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.gfinal.map.common.Constant;
import com.gfinal.map.place.Location;
import com.gfinal.map.util.HttpUtils;

/**
 * @author He Tao (ht@nbicc.com)
 * @date 2015年3月2日
 */
public class GeoPlaceCode {

	private String id;
	private GeoPlaceBean geoPlaceBean = null;
	private ArrayList<GeoPlaceBean> geoList;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private String createURI() {
		ArrayList params = new ArrayList();
		params.add(new BasicNameValuePair("id", id));
		params.add(new BasicNameValuePair("ak", Constant.BAIDU_AK));
		return Constant.GEOPLACECODE_URL + "?"
				+ URLEncodedUtils.format(params, Constant.CHARSET);
	}

	@SuppressWarnings("rawtypes")
	public ArrayList<GeoPlaceBean> execute() {
		geoList = new ArrayList<GeoPlaceBean>();
		SAXReader reader = new SAXReader();
		String response = HttpUtils.doGet(createURI());
		Document doc;
		try {
			ByteArrayInputStream in = new ByteArrayInputStream(
					response.getBytes("UTF-8"));
			doc = reader.read(in);
			Element root = doc.getRootElement(); // 获取根节点
			List<Element> list = new ArrayList<Element>();
			list.add(root);
			while (list.size() > 0) {
				Element element = null;
				Element ele = null;
				Iterator ite = list.iterator();
				if (ite.hasNext()) {
					ele = (Element) ite.next();
					ite.remove();
				}
				if (ele != null) {
					
					Location location = null;

					for (Iterator i = ele.elementIterator(); (i != null)
							&& (i.hasNext());) {
						element = (Element) i.next();
						list.add(element);

						if (element != null) {
							if(element.getName().equals("status")){
								if(!element.getText().equals("Success")){
									System.out.println(id+"   "+element.getText());
								}
							}
							else if (element.getName().equals("name")) {
								geoPlaceBean = new GeoPlaceBean();
								geoPlaceBean.setName(element.getText());
							} else if (element.getName().equals("lng")) {
								location = new Location();
								location.setLng(Float.valueOf(element.getText()));
							} else if (element.getName().equals("lat")) {
								location.setLat(Float.valueOf(element.getText()));
								geoPlaceBean.setLocation(location);
								geoList.add(geoPlaceBean);
							}
						}
					}
				}

				if (geoList.size() == 1) {
					break;
				}

			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < geoList.size(); i++) {
			System.out.println(geoList.get(i).toString());
		}

		return geoList;

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		GeoPlaceCode geoPlaceCode = new GeoPlaceCode();
		geoPlaceCode.setId("longwanggong");
		geoPlaceCode.execute();
		geoPlaceCode.setId("penglaige");
		geoPlaceCode.execute();
	}

}
